<?

	class FTALightBox extends FTAObject {
		
		var $cmsDocument;//cms document needs a tart up.. to be nicer cache friendly..
		var $galleryImages;
		var $gallery;
		
		var $imageElementId;
		var $enlargementFileType;
		var $thumnailFileType;
		
		var $hasInsertedLightBoxJS;
		
		function FTALightBox() {
			parent::FTAObject();
			
			global $db;	
			$this->cmsDocument = new CmsDocument($db);
			//set up the base options on the cms document.. so we only need to do it once??
			global $FTALightBoxImageElementId;
			$this->imageElementId = (0 < $FTALightBoxImageElementId) ? $FTALightBoxImageElementId : 11362;
			
			global $FTALightBoxEnlargementFileType;
			$this->enlargementFileType = (0 < $FTALightBoxEnlargementFileType) ? $FTALightBoxEnlargementFileType : 1;
			
			global $FTALightBoxThumbnailFileType;
			$this->thumnailFileType = (0 < $FTALightBoxThumbnailFileType) ? $FTALightBoxThumbnailFileType : 1;
			
			$this->hasInsertedLightBoxJS = false;
		
		}
		
		function openGallery($galleryIdentifier){
	
			
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docFileMod');
			$this->cmsDocument->addConstraint('author',1);

			if (is_numeric( ($galleryIdentifier * 1) ) &&  0 < ($galleryIdentifier * 1)){
				$this->cmsDocument->addConstraint('documentcode',$galleryIdentifier);	
			} else {
				$this->cmsDocument->addConstraint('title',$galleryIdentifier);
			}
			
			$this->cmsDocument->setPageSize(1);
			
			$this->cmsDocument->resetIterator();
			
			if($this->cmsDocument->totalDocuments() == 1) {
				$this->gallery = $this->cmsDocument->nextDocumentFromCache();

				return true;
			} else {
				$this->gallery = array();				
				return false;
			}
		}
		
		
		function openGalleryImages() {
			
			if  (!$this->gallery)
				return false;
				
			$this->cmsDocument->flush();
			$this->cmsDocument->setActiveDocuments();
			$this->cmsDocument->addDocumentType('docFileMod');
			$this->cmsDocument->addConstraint('author',0);			
			$this->cmsDocument->addConstraint('categorycode',$this->gallery->documentcode);
			$this->cmsDocument->addSort('<<rank>>');
			
			$this->cmsDocument->setPageSize(null);
			
			$this->cmsDocument->resetIterator();
			
			$this->galleryImages = $this->cmsDocument->getAllDocuments();
			
			if (!$this->galleryImages) {
				$this->galleryImages = array();
				return false;
			}
			
			return true;
		}
		
		
		function render(){

?>
<ul id="fta_LightBox_<?= $this->gallery->documentcode ?>" class="fta_lightbox">
<?
			foreach($this->galleryImages as $image){

				$imageThumbNailUrl = "/html/blob.php?documentCode=" . $image->documentcode . "&elementId=". $this->imageElementId . "&filetypecode=" . $this->thumnailFileType;
				$imageEnlargementUrl = "/html/blob.php?documentCode=" . $image->documentcode . "&elementId=". $this->imageElementId . "&filetypecode=" . $this->enlargementFileType;
				
?>
		<li><a href="<?= $imageEnlargementUrl ?>" rel="lightbox[<?= $this->gallery->documentcode ?>]"><img src="<?= $imageThumbNailUrl ?>"  alt="<?= $image->title ?>" border="0" /></a></li>
<?
			}
?>
</ul>
<?			
		
			
		if (!$this->hasInsertedLightBoxJS) {
				$this->hasInsertedLightBoxJS = true;
?>
<script type="text/javascript" src="/js/lightbox.js"></script>
<?				
		}		
		
		
		}
	}
	
	
////////


	function fta_lightbox($galleryIdentifier) {		
		ob_start();
		$ftaLightBox = &fta_getSharedLightboxObject();		
		if ( $ftaLightBox->openGallery($galleryIdentifier) && $ftaLightBox->openGalleryImages()){
			$ftaLightBox->render();
		}
		return ob_get_clean();
	}
	
	function &fta_getSharedLightboxObject(){

		global $fta_;

		global $FTALightBoxClass;
		
		if(!is_null($FTALightBoxClass) && "FTALightBox" != $FTALightBoxClass){
		} else {
			$FTALightBoxClass = "FTALightBox";
		}
			
		if (!array_key_exists('ftaLightBox',$fta_)){
			$fta_->ftaLightBox = new  $FTALightBoxClass();
		}
		return $fta_->ftaLightBox;
	}